//
//  CountryController.m
//  NewGeneration
//
//  Created by tikpen on 3/26/20.
//  Copyright © 2020 tikpen. All rights reserved.
//



#import "CountryController.h"
#import "CountryTableViewCell.h"
#import "ReadLocalJsonFile.h"

static NSString *CountryTableViewCellId = @"CountryTableViewCellId";

@interface CountryController ()
@property (weak, nonatomic) IBOutlet UITableView *dataTableView;
@property (nonatomic, strong) NSMutableArray *dataArray;
@end

@implementation CountryController

- (void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];
    self.navigationController.navigationBarHidden = NO;
    self.navigationController.navigationBar.tintColor = UIColor.blackColor;
    self.navigationController.navigationBar.titleTextAttributes = @{NSForegroundColorAttributeName:UIColor.blackColor};
}

- (void)viewWillDisappear:(BOOL)animated{
    [super viewWillDisappear:animated];
    self.navigationController.navigationBar.tintColor = UIColor.whiteColor;
    self.navigationController.navigationBar.titleTextAttributes = @{NSForegroundColorAttributeName:UIColor.whiteColor};
}

- (void)viewDidLoad {
    [super viewDidLoad];
    self.title = @"选择国家或地区";
    NSArray *tempArr = [ReadLocalJsonFile readLocalFileWithName:@"country"];
    [self.dataArray addObjectsFromArray:tempArr];
    
    
    [self configTableView];
}

- (void)configTableView{
    self.dataTableView.tableFooterView = [UIView new];
    self.dataTableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    [self.dataTableView registerNib:[UINib nibWithNibName:@"CountryTableViewCell" bundle:nil] forCellReuseIdentifier:CountryTableViewCellId];
    
}
#pragma mark - UITableViewdelegate -
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return [self.dataArray count];
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    CountryTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CountryTableViewCellId forIndexPath:indexPath];
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    NSDictionary *model = self.dataArray[indexPath.row];
    [cell setupData: model];
    return cell;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    if (self.delegate && [self.delegate respondsToSelector:@selector(countryNumChoosed:)]) {
        NSDictionary *model = self.dataArray[indexPath.row];
        [self.delegate countryNumChoosed:[NSString stringWithFormat:@"%@",model[@"code"]]];
    }
    [self.navigationController popViewControllerAnimated:YES];
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return 40;
}

- (NSMutableArray *)dataArray{
    if (!_dataArray) {
        _dataArray = [NSMutableArray array];
    }
    return _dataArray;
}
@end
